package pl.jad.database.table.row;

import pl.jad.database.table.Table;
import pl.jad.database.table.column.TableColumn;

/**
 * Table row
 * Represents object stored in table.
 *
 */
public class TableRow {
	protected Table _table;
	
	protected Object _object;
	
	public TableRow(Table table, Object object) {
		if (table == null || object == null) {
			throw new NullPointerException("Table and object can NOT be null");
		}
		
		this._table = table;
		this._object = object;
	}
	
	public Table getTable() {
		return this._table;
	}
	
	protected String getInsertSql(Object obj) throws Exception {
		StringBuilder sb = new StringBuilder();
		
		sb.append("INSERT INTO " + this._table.getName() +"(");
		for (TableColumn column : this._table.getColumns()) {
			sb.append(column.getName() + ", ");
		}
		sb.delete(sb.length() - 2, sb.length());
		
		sb.append(") VALUES (");
		
		for (TableColumn column : this._table.getColumns()) {
			sb.append(column.getSqlFieldValue(obj) + ", ");
		}
		sb.delete(sb.length() - 2, sb.length());
		
		sb.append(");");
		return sb.toString();
	}
	
	public String getInsertSql() throws Exception {		
		return getInsertSql(this._object);
	}
}
